package cn.YstAdmin.dao.impl;

import cn.YstAdmin.dao.YstAdminDao;
import cn.controller.AopController;
import cn.util.aliyun.AliyunOSSClientUtil;
import common.service.JedisClient;
import common.service.MryeDataCenterService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component("ystAdminDao")
public class YstAdminDaoImpl implements YstAdminDao {


    //数据源
    @Resource(name="mryeDataCenterService")
    MryeDataCenterService mryeDataCenterService;

    //OSS
    @Autowired
    AliyunOSSClientUtil aliyunOSSClientUtil;

    //Redis
    @Resource(name="jedisClient")
    JedisClient jedisClient;

    //日志
    private Logger logger = LoggerFactory.getLogger(YstAdminDaoImpl.class);



    /**
     * 获取基本信息
     * @param params
     * @return
     */
    public Map<String, Object> getYstInfo(Map<String, Object> params) {
        String sql="SELECT INFO_LOGO AS SY_LOGO, INFO_COPYRIGHT AS SY_BQ, INFO_RECORD AS SY_BA, INFO_OTHER AS SY_QT, INFO_SMTP_IP AS YX_IP, INFO_SMTP_PORT AS YX_DK, INFO_SMTP_ADDRESS AS YX_DZ, INFO_SMTP_USER AS YX_USER, INFO_SMTP_PWD AS YX_PWD FROM YST_INFO";
        params.put("sql",sql);
        return mryeDataCenterService.searchSap(params);
    }

    /**
     * 获取阿里云OSS
     * @param params
     * @return
     */
    public Map<String, Object> getYstALiOSS(Map<String, Object> params) {
        String sql="SELECT OSS_ENDPOINT AS OSS_URL, OSS_ACCESS_KEY_ID AS OSS_KEY, OSS_ACCESS_SECRET AS OSS_PWD, OSS_BUCKETNAME AS OSS_MC, OSS_FILEDIR AS OSS_ML FROM YST_API_OSS";
        params.put("sql",sql);
        return mryeDataCenterService.searchSap(params);
    }

    /**
     * 获取Redis信息
     * @param params
     * @return
     */
    public Map<String, Object> getYstRedis(Map<String, Object> params) {
        String sql="SELECT REDIS_HOST AS REDISYM, REDIS_PORT AS REDISDK, REDIS_PWD AS REDISMM, REDIS_TYPE AS REDISLX, REDIS_SHOW AS REDISXS FROM YST_SYS_REDIS";
        params.put("sql",sql);
        return mryeDataCenterService.searchSap(params);
    }
    /**
     * 修改系统设置
     * @param params
     * @return
     */
    public int updateadminSystemBase(Map<String, Object> params) {
        int result=0;
        String sql="UPDATE YST_INFO SET INFO_LOGO=#{SY_LOGO},INFO_COPYRIGHT=#{SY_BQ},INFO_RECORD=#{SY_BA},INFO_OTHER=#{SY_QT},INFO_SMTP_IP=#{YX_IP},INFO_SMTP_PORT=#{YX_DK},INFO_SMTP_ADDRESS=#{YX_DZ},INFO_SMTP_USER=#{YX_USER},INFO_SMTP_PWD=#{YX_PWD};" ;
              params.put("sql",sql);
        result=mryeDataCenterService.updateSap(params);
               sql="UPDATE YST_API_OSS SET OSS_ENDPOINT=#{OSS_URL},OSS_ACCESS_KEY_ID=#{OSS_KEY},OSS_ACCESS_SECRET=#{OSS_PWD},OSS_BUCKETNAME=#{OSS_MC},OSS_FILEDIR=#{OSS_ML};";
               params.put("sql",sql);
        result=mryeDataCenterService.updateSap(params);
              sql= "UPDATE YST_SYS_REDIS SET REDIS_HOST=#{REDISYM},REDIS_PORT=#{REDISDK},REDIS_PWD=#{REDISMM},REDIS_TYPE=#{REDISLX},REDIS_SHOW=#{REDISXS}";
              params.put("sql",sql);
        result=mryeDataCenterService.updateSap(params);

        //把OSS和Redis 更新
        if(result>0){
            Map<String,Object> map=new HashMap<>();
            //更新OSS状态
            map.put("sql","SELECT OSS_ENDPOINT,OSS_ACCESS_KEY_ID,OSS_ACCESS_SECRET,OSS_BUCKETNAME,OSS_FILEDIR FROM YST_API_OSS");
            Map<String,Object> OSS=mryeDataCenterService.searchSap(map);
            if(OSS!=null){
                try{
                    aliyunOSSClientUtil.endpoint= (String) OSS.get("OSS_ENDPOINT");
                    aliyunOSSClientUtil.accessKeyId=(String) OSS.get("OSS_ACCESS_KEY_ID");
                    aliyunOSSClientUtil.accessKeySecret=(String) OSS.get("OSS_ACCESS_SECRET");
                    aliyunOSSClientUtil.bucketName=(String) OSS.get("OSS_BUCKETNAME");
                    aliyunOSSClientUtil.filedir=(String) OSS.get("OSS_FILEDIR");
                    //AliyunOSSClientUtil.UploadOSS(new File("D:\\66.jpg"),1);
                    logger.info("更新阿里云OSS对象存储-----成功！");
                }catch (Exception e){
                    logger.info("更新阿里云OSS对象存储-----失败！");
                }
            }
            //更新Redis状态
            map.put("sql","SELECT REDIS_ID,REDIS_HOST,REDIS_PORT,REDIS_PWD,REDIS_TYPE,REDIS_SHOW FROM YST_SYS_REDIS WHERE REDIS_SHOW=1");
            Map<String,Object> REDIS=mryeDataCenterService.searchSap(map);
            if (REDIS!=null&&REDIS.size()>0){
                try{
                    jedisClient.init(REDIS);
                    jedisClient.set("init","HelloRedis");
                    logger.info("更新牛逼的Redis缓存------成功！ "+jedisClient.get("init"));
                }catch (Exception e){
                    logger.info("更新牛逼的Redis缓存------失败！----------------------- ");
                }

            }


        }
        return result;
    }

    /**
     * Shiro过滤链 加载
     * @param params
     * @return
     */
    public List<Map<String, Object>> getYstShiro(Map<String, Object> params) {
       String sql="SELECT SHIRO_ID,SHIRO_URL,SHIRO_NAME,SHIRO_POWER,SHIRO_SHOW FROM YST_SYS_SHIRO WHERE SHIRO_SHOW=1";
       params.put("sql",sql);
       return mryeDataCenterService.searchSapList(params);
    }

    @Override
    public List<Map<String, Object>> getadminRole(Map<String, Object> params) {
        String sql="SELECT `ID`, `ROLE_NICK` AS JSBM, `ROLE_NAME` AS JSMC, `ROLE_DES` AS JSSM FROM YST_ROLE";
        params.put("sql",sql);
        return mryeDataCenterService.searchSapList(params);
    }
}
